package com.epalmpay.service.common.impl;

import com.alibaba.fastjson.JSON;
import com.epalmpay.entity.ExpressDetail;
import com.epalmpay.entity.OrderGood;
import com.epalmpay.entity.Response;
import com.epalmpay.mapper.ExpressShortnameMapper;
import com.epalmpay.mapper.OrderGoodMapper;
import com.epalmpay.service.common.IExpressInfoService;
import com.epalmpay.util.DateUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import com.epalmpay.util.HttpUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExpressInfoServiceImpl implements IExpressInfoService {
    @Resource
    private ExpressShortnameMapper expressShortnameMapper;
    @Resource
    private OrderGoodMapper orderGoodMapper;

    @Override
    public Response getExpressShortName(String name) {
        List<Map<String, String>> shortNames = expressShortnameMapper.getByName(name);
        return Response.getSuccess(shortNames);
    }

    @Override
    public Response getExpressDetail(Long orderId) {
        Date date = new Date();
        Date canSendRequest = DateUtil.getAfterHour(date, -2);
        Map<String, Object> expressInfo = orderGoodMapper.getExpressInfoById(orderId);
        if(expressInfo == null || expressInfo.get("expressNo") == null || expressInfo.get("expressShort") == null) {
            return Response.getError("请求非法");
        }

        String detailStr;
        if(expressInfo.get("expressDetail") == null || canSendRequest.after((Date) expressInfo.get("gmtModified"))) {
            String expressNo = (String) expressInfo.get("expressNo");
            String expressShort = (String) expressInfo.get("expressShort");

            detailStr = getDetail(expressNo, expressShort);

            if(detailStr != null) {
                OrderGood orderGood = new OrderGood();
                orderGood.setId((Long) expressInfo.get("id"));
                orderGood.setGmtModified(date);
                orderGood.setExpressDetail(detailStr);
                orderGoodMapper.updateByPrimaryKeySelective(orderGood);
            } else {
                return Response.getError("服务器错误");
            }

        } else {
            detailStr = (String) expressInfo.get("expressDetail");
        }

        ExpressDetail expressDetail = JSON.parseObject(detailStr, ExpressDetail.class);
        return Response.getSuccess(expressDetail);
    }

    public String getDetail(String expressNo, String expressShort) {
        String host = "https://ali-deliver.showapi.com";
        String path = "/showapi_expInfo";
        String method = "GET";
        String appcode = "c3ec3dfb51ce4e9aab753381d6c61743";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("com", expressShort);
        querys.put("nu", expressNo);


        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String resultStr = EntityUtils.toString(response.getEntity());
            Map result = JSON.parseObject(resultStr);
            if((Integer)result.get("showapi_res_code") == 0) {
                return JSON.toJSONString(result.get("showapi_res_body"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
